const {
  articleModels,
  messageModels,
  praiseModels,
  commentModels,
  fileModels,
} = require("../../models");
const mkdir = require("../../lib/mkdir");
//获取文章
exports.getArticle = async function (req, res) {
  try {
    let data = req.body;
    let count = -1;
    await articleModels
      .getArticle(
        data.pageSize,
        data.nowPage,
        data.state,
        data.subset_id,
        data.searchTerm,
        data.classify
      )
      .then(async (result) => {
        if (data.count) {
          let c = await articleModels.getArticleCount(
            data.state,
            data.subset_id,
            data.searchTerm,
            data.classify
          );
          count = c[0].count;
        }
        if (result.length > 0) {
          for (let i = 0; i < result.length; i++) {
            let praise = await praiseModels.getPraiseCount(result[i].id, -1); //获取点赞数量
            let comment = await commentModels.getCommentCount(result[i].id); //获取评论数量
            result[i].praise = praise[0].count;
            result[i].comment = comment[0].count;
            if (result[i].label) {
              result[i].label = result[i].label.split(","); //将标签字符串转换为数组的操作
            }
          }
        }
        res.send({
          code: 200,
          data: {
            result,
            count,
          },
        });
      });
  } catch (error) {
    res.send({
      code: 400,
      data: error,
    });
  }
};

//修改文章发布状态
exports.updateArticleState = async function (req, res) {
  try {
    let data = req.body;
    await articleModels
      .updateArticleState(data.id, data.state)
      .then((result) => {
        res.send({
          code: 200,
          data: result,
        });
      });
  } catch (error) {
    res.send({
      code: 400,
      data: error,
    });
  }
};

//删除文章
exports.deleteArticle = async function (req, res) {
  try {
    let data = req.body;
    // 删除文章/图库的图片文件
    if (data.fileUrl.length > 0) {
      await fileModels.deleteFileByPath(data.fileUrl);
      if (typeof data.fileUrl === "string") {
        const filePath = `data/files/${data.fileUrl}`;
        await mkdir.delFiles(filePath);
      } else {
        data.fileUrl.forEach(async (url) => {
          const filePath = `data/files/${url}`;
          await mkdir.delFiles(filePath);
        });
      }
    }
    // 删除文章
    await articleModels.deleteArticle(data.id).then((result) => {
      res.send({
        code: 200,
        data: result,
      });
    });
  } catch (error) {
    res.send({
      code: 400,
      data: error,
    });
  }
};

//查询文章不同状态下的条数
exports.getArticleStateCount = async (req, res) => {
  let unpublish = await articleModels.getArticleCount(0, -1, "", 0);
  let publish = await articleModels.getArticleCount(1, -1, "", 0);
  let message = [
    {
      id: 0,
      name: "未发布",
      value: unpublish[0].count,
    },
    {
      id: 1,
      name: "已发布",
      value: publish[0].count,
    },
  ];
  res.send({ code: 200, data: message });
};

//新建文章/图库
exports.addArticle = async (req, res) => {
  try {
    let data = req.body;
    if (data.value.label) {
      //将标签数组转换为字符串的操作
      data.value.label = data.value.label.join(",");
    }
    await articleModels.addArticle(data.value).then((result) => {
      res.send({
        code: 200,
        id: result.insertId,
      });
    });
  } catch (error) {
    res.send({
      code: 400,
      data: error,
    });
  }
};

//根据id获取文章
exports.getArticleById = async (req, res) => {
  try {
    let data = req.body;
    await articleModels.getArticleById(data.articleId).then((result) => {
      res.send({
        code: 200,
        data: result,
      });
    });
  } catch (error) {
    res.send({
      code: 400,
      data: error,
    });
  }
};

//修改文章、图库
exports.updateArticle = async (req, res) => {
  try {
    let data = req.body;
    if (data.value.label) {
      //将标签数组转换为字符串的操作
      data.value.label = data.value.label.join(",");
    }
    await articleModels.updateArticle(data.id, data.value).then((result) => {
      res.send({
        code: 200,
        data: result,
      });
    });
  } catch (error) {
    res.send({
      code: 400,
      data: error,
    });
  }
};

//跳转编辑文章页面获取文章详情
exports.getArticleById = async (req, res) => {
  let data = req.body;
  try {
    await articleModels.getArticleById(data.id).then((result) => {
      res.send({
        code: 200,
        data: result[0],
      });
    });
  } catch (error) {
    res.send({
      code: 400,
      data: error,
    });
  }
};
